package com.jebel.framwork.common.util;


public class StringUtil {

    public static boolean areNotEmpty(String[] values) {
        boolean result = true;
        if ((values == null) || (values.length == 0))
            result = false;
        else {
            for (String value : values) {
                result &= !isEmpty(value);
            }
        }
        return result;
    }

    public static boolean isEmpty(String value) {
        int strLen;
        if ((value == null) || ((strLen = value.length()) == 0))
            return true;
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(value.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 将字符串替换成首位带*号形式
     *
     * @param str
     * @return
     */
    public static String codeContent(String str) {
        if (isEmpty(str)) {
            return null;
        }
        int len = str.length();
        int start = 0;
        int end = 1;
        String code = "";
        if (len == 1) {
            return "*";
        } else if (len == 2) {
            end = len - 1;
        } else if (len >= 3 && len < 11) {
            start = (len - 1) / 2;
            end = (len - 3) / 2;
        } else if (len >= 11 && len < 18) {
            start = 3;
            end = 4;
        } else if (len == 18) {
            start = 6;
            end = 4;
        } else {
            start = len / 2 - 5;
            end = 4;
        }
        int codeLength = len - (start + end);
        if (len > 0) {
            for (int i = 0; i < codeLength; i++) {
                code += "*";
            }
        }
        return str.replaceAll("(.{" + start + "})(.*)(.{" + end + "})", "$1" + code + "$3");
    }

    public static String unescape(String input) {
        int len = input.length();
        StringBuffer result = new StringBuffer();
        result.ensureCapacity(len);
        int lastPos = 0, pos = 0;
        char ch;
        while (lastPos < len) {
            pos = input.indexOf("%", lastPos);
            if (pos == lastPos) {
                if (input.charAt(pos + 1) == 'u') {
                    ch = (char) Integer.parseInt(input.substring(pos + 2, pos + 6), 16);
                    result.append(ch);
                    lastPos = pos + 6;
                } else {
                    ch = (char) Integer.parseInt(input.substring(pos + 1, pos + 3), 16);
                    result.append(ch);
                    lastPos = pos + 3;
                }
            } else {
                if (pos == -1) {
                    result.append(input.substring(lastPos));
                    lastPos = len;
                } else {
                    result.append(input.substring(lastPos, pos));
                    lastPos = pos;
                }
            }
        }
        return result.toString();
    }

    public static String toGb2312(String input) {
        input = input.trim().replaceAll("(?i)\\\\u", "%u");
        return unescape(input);
    }
}
